package com.youme.section.shop;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.hjq.toast.ToastUtils;
import com.hyphenate.easeim.R;
import com.salton123.ui.base.YoumeTitleActivity;
import com.turntableview.ITurntableListener;
import com.turntableview.TurntableView;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

public class ChoujiangActivity extends YoumeTitleActivity {
    private ImageView mGo;

    private TurntableView turntableView;
    private ArrayList<Integer> mColors = new ArrayList<>();
    private ArrayList<Integer> postions = new ArrayList<>();
    private ImageView relafsh;
    private EditText playNuma;
    private Button sure;
    private int num = 0;
    private double price = 0.0;
    ArrayList<String> nameList = new ArrayList<>();
    ArrayList<Bitmap> bitmapList = new ArrayList<>();

    @Override
    public int getLayoutId() {
        return R.layout.layout_choujiang;
    }

    @Override
    public String getTitleText() {
        return "支付游戏";
    }

    @Override
    public void initViewAndData() {
        Intent intent = getIntent();
        price = intent.getDoubleExtra("price", 0.0);
        mGo = findViewById(R.id.iv_node);
        turntableView = findViewById(R.id.turntable);
        mColors.add(getResources().getColor(R.color.appColor));
        mColors.add(getResources().getColor(R.color.default_login_color));
        mColors.add(getResources().getColor(R.color.default_login_color_stub));
        turntableView.setBackColor(mColors);
        relafsh = findViewById(R.id.im_reflash);
        playNuma = findViewById(R.id.play_num);
        sure = findViewById(R.id.sure);
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                InputMethodManager inputMethodManager =
                    (InputMethodManager) getBaseContext().getSystemService(Activity.INPUT_METHOD_SERVICE);
                inputMethodManager
                    .hideSoftInputFromWindow(playNuma.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                if (playNuma.getText().toString().isEmpty()) {
                    ToastUtils.show("请输入游戏人数");
                    return;
                }
                num = Integer.parseInt(playNuma.getText().toString().trim());
                if (num < 2) {
                    ToastUtils.show("参与游戏至少两人！！！");
                } else {
                    for (int j = 0; j < num; j++) {
                        postions.add(j);
                    }
                    Collections.shuffle(postions);
                    sure.setText(playNuma.getText().toString());
                    sure.setBackgroundResource(R.drawable.shape_no_jiesuan_bg);
                    sure.setEnabled(false);
                    nameList.add("50%");
                    nameList.add("免单");
                    bitmapList.add(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
                    bitmapList.add(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
                    int playnum = num - 2;
                    if (playnum > 0) {
                        int pingjun = 50 / playnum;
                        int other = 50 - pingjun * playnum;
                        for (int i = 0; i < playnum - 1; i++) {
                            nameList.add(playnum + "%");
                            bitmapList.add(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
                        }
                        nameList.add((pingjun + other) + "%");
                        bitmapList.add(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
                    }
                    turntableView.setDatas(num, nameList, bitmapList);
                }
            }
        });

        Button btGoPay = findViewById(R.id.pay_for_end);
        btGoPay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ChoujiangActivity.this, PayforActivity.class);
                intent.putExtra("price", price);
                startActivity(intent);
            }
        });
        relafsh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                nameList.clear();
                bitmapList.clear();
                num = 0;
                postions.clear();
                sure.setEnabled(true);
                sure.setText("确定");
                playNuma.setText("");
                playNuma.setHint("请输入游戏人数");
                sure.setBackgroundResource(R.drawable.shape_jiesuan_bg);
            }
        });

//        bitmapList.add(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));

        mGo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (playNuma.getText().toString().isEmpty()) {
                    ToastUtils.show("请输入游戏人数");
                    return;
                }
                if (!(Integer.parseInt(playNuma.getText().toString().trim()) > 1)) {
                    ToastUtils.show("参与游戏至少两人！！！");
                    return;
                }
                if (num == 0) {
                    return;
                }
                num--;
                sure.setText("" + num);
                turntableView.startRotate(postions.get(num), new ITurntableListener() {
                    @Override
                    public void onStart() {
                        postions.remove(num);
                        Toast.makeText(ChoujiangActivity.this, "开始抽奖", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onEnd(int position, String name) {

                        Toast.makeText(ChoujiangActivity.this, "结束", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }

    private int getNum(int endNum) {
        if (endNum > 0) {
            Random random = new Random();
            return random.nextInt(endNum);
        }
        return 0;
    }
}
